﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Components.Color.Types;
using RayDen.Library.Components.Surface;
using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Corrector.Managers;

namespace RayEngine.Spectral.Entities.Brdfs
{
    public class LambertBrdf : BrdfBase
    {
        public override BrdfType Type
        {
            get { return BrdfType.Diffuse; }
        }

        public override BrdfClass Class
        {
            get { return BrdfClass.DiffuseLambert; }
        }


        public override float Pdf(ref Vector wo, ref Vector wi)
        {
            return MathLab.INVPI;
        }


        public override void F(ShadePointInfo pt, out IColorType fs, BrdfType types = BrdfType.Diffuse)
        {
            fs = pt.Diffuse.Mul(MathLab.INVPI);
        }

        public override void Sample_f(ShadePointInfo point, float u0, float u1, float u2, out Vector wi, out float radiance, out float pdf, out BsdfEvent bsdfEvent)
        {
            Vector dir = MC.CosineSampleHemisphere(u0, u1);
            pdf = dir.z * MathLab.INVPI;
            bsdfEvent = BsdfEvent.Diffuse;
            wi = point.Frame.ToWorld(ref dir);
            float dp = (Normal.Dot(ref point.GeoNormal, ref wi));
            if (dp <= 0.01f)
            {
                pdf = 0f;
                bsdfEvent = BsdfEvent.Absorb;
                radiance = 0f;
            }
            else
            {
                pdf /= dp;
                radiance = MathLab.INVPI;
            }
        }
    }
}
